#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5e5 + 5;
const int INF = (1ll << 31) - 1;
int n, m;
struct Node {
  int val, pri, sz;
  int ch[2];
} tr[N * 80];
#define val(p) tr[p].val
#define pri(p) tr[p].pri
#define sz(p) tr[p].sz
#define lc(p) tr[p].ch[0]
#define rc(p) tr[p].ch[1]
int rt[N];
int newnode(int p) {
  static int tot = 0;
  ++tot;
  val(tot) = val(p);
  pri(tot) = pri(p);
  sz(tot) = sz(tot);
  lc(tot) = lc(p);
  rc(tot) = rc(p);
  return tot;
}
int newnode(int p, int v) {
  p = newnode(0);
  val(p) = v;
  pri(p) = rand();
  sz(p) = 1;
  lc(p) = rc(p) = 0;
  return p;
}
void pushup(int p) { sz(p) = sz(lc(p)) + sz(rc(p)) + 1; }
void split(int p, int v, int& x, int& y) {
  if (!p) {
    x = y = 0;
    return;
  }
  p = newnode(p);
  if (val(p) <= v) {
    x = p;
    split(rc(x), v, rc(x), y);
    pushup(x);
  } else {
    y = p;
    split(lc(y), v, x, lc(y));
    pushup(y);
  }
}
int merge(int x, int y) {
  if (!x || !y) return x + y;
  if (pri(x) >= pri(y)) {
    x = newnode(x);
    rc(x) = merge(rc(x), y);
    pushup(x);
    return x;
  } else {
    y = newnode(y);
    lc(y) = merge(x, lc(y));
    pushup(y);
    return y;
  }
}
void insert(int& p, int v) {
  int x, y, z;
  split(p, v, x, z);
  y = newnode(0, v);
  p = merge(x, merge(y, z));
}
void del(int& p, int v) {
  int x, y, z;
  split(p, v, x, z);
  split(x, v - 1, x, y);
  y = merge(lc(y), rc(y));
  p = merge(x, merge(y, z));
}
int get_rk(int& p, int v) {
  int x, y, z;
  split(p, v - 1, x, y);
  int res = sz(x);
  p = merge(x, y);
  return res;
}
int get_val(int p, int k) {
  if (k <= sz(lc(p)))
    return get_val(lc(p), k);
  else if (k <= sz(lc(p)) + 1)
    return val(p);
  else
    return get_val(rc(p), k - sz(lc(p)) - 1);
}
int get_pre(int& p, int v) {
  int x, y;
  split(p, v - 1, x, y);
  int res = get_val(x, sz(x));
  p = merge(x, y);
  return res;
}
int get_suc(int& p, int v) {
  int x, y;
  split(p, v, x, y);
  int res = get_val(y, 1);
  p = merge(x, y);
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  insert(rt[0], INF);
  insert(rt[0], -INF);
  cin >> n;
  rep(i, 1, n) {
    int v, op, x;
    cin >> v >> op >> x;
    rt[i] = rt[v];
    if (op == 1)
      insert(rt[i], x);
    else if (op == 2)
      del(rt[i], x);
    else if (op == 3)
      x = get_rk(rt[i], x);
    else if (op == 4)
      x = get_val(rt[i], x + 1);
    else if (op == 5)
      x = get_pre(rt[i], x);
    else
      x = get_suc(rt[i], x);
    if (op >= 3 && op <= 6) cout << x << endl;
  }
  return 0;
}